package cz.zcu.fav.ratdroid.activity;

import java.util.ArrayList;
import java.util.List;

import cz.zcu.fav.ratdroid.bean.Object;
import cz.zcu.fav.ratdroid.service.RestService;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
/**
 * Activity just show all favourites corresponding to currently logged user.
 * 
 * @author Lukas Gemela
 *
 */
public class FavouritesActivity extends AbstractObjectsListActivitity
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		getListView().setOnItemClickListener(new OnItemClickListener()
		{
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id)
			{
				Intent intent = new Intent(FavouritesActivity.this, ObjectProfileActivity.class);
				intent.putExtra(Object.SINGLE_OBJECT_IDENTIFIER, getLoadedObjects().get(position));
				startActivityForResult(intent, ObjectProfileActivity.OBJECT_INVALIDATED_REQUEST_CODE);
			}
		});
		
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (requestCode == ObjectProfileActivity.OBJECT_INVALIDATED_REQUEST_CODE)
		{
			if (resultCode == android.app.Activity.RESULT_OK)
			{
				setRestTask(RestService.getInstance().getFavourites(this, this));
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	@Override
	protected List<Object> provideData()
	{
		// provide empty data for now but initialize async task which will
		// change our model later
		setRestTask(RestService.getInstance().getFavourites(this, this));
		return new ArrayList<Object>();
	}
}
